//
//  ViewController.m
//  Jared_AFN_Cache
//
//  Created by Jared on 2017/1/18.
//  Copyright © 2017年 Jared. All rights reserved.
//

#import "ViewController.h"
#import "JD_AFNetworking.h"
#import "JDRequest.h"
#import "Model.h"
#import "JD_CommonRequest.h"
#import "Lists.h"
#import "ListModelFrame.h"
#import "MJExtension.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)NSArray *dataArray;
@property (nonatomic, strong)NSArray *listModelArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTableView];
    [self loadData];
}

- (void)setUpTableView{
    self.title = @"列表";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview: self.tableView];
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
    [button setTitle:@"清除缓存" forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(rightBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = leftBarBtn;
}

- (void)loadData{
    NSLog(@"hahah");
    /*
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSLog(@"缓存目录===%@", path);
    __weak __typeof (self)weakSelf = self;
    JD_CommonRequest *jdRequest = [[JD_CommonRequest alloc] init];
    
    [jdRequest requestListDataWithURL:@"http://appapi.makeronly.com/MKLService/featuredfirst/banner" params:nil requestType:NetworkGetType cacheType:JDCacheTypeReturnCacheDataThenLoad success:^(CommonResponseBody *responseBody) {
         weakSelf.dataArray = responseBody.responseBean.data;
        Model *model = weakSelf.dataArray[0];
        NSLog(@"model.assetUrl====%@", model.assetUrl);
//        [self.tableView reloadData];
    } failure:^(CommonResponseBean *result) {
        
    }];
    //清楚缓存
//    [JD_CacheTool clearCache];
     
    */
    
    
    NSDictionary *param = @{
                            @"auth" : @"W8c8Fivl9flDCsJzH8HukzJxQMrm3N7KP9Wc5WTFjcWP229VKTBRU7vI",
                            @"client" : @"1",
                            @"deviceid" : @"A55AF7DB-88C8-408D-B983-D0B9C9CA0B36",
                            @"limit" : @"10",
                            @"start" : @0,
                            @"version":@"3.0.6"
                            };
    JD_CommonRequest *request = [[JD_CommonRequest alloc] init];
    [request requestDataWithURL:@"http://api2.pianke.me/timeline/list" params:param requestType:NetworkPostType cacheType:JDCacheTypeReturnCacheDataThenLoad success:^(CommonResponseBody *responseBody) {
        NSArray *listModel = [Lists mj_objectArrayWithKeyValuesArray:responseBody.responseBean.data];
        NSLog(@"listModellistModel==%@", listModel);
        self.listModelArray = [self listFrameWithListModel:listModel];
        
        NSLog(@"responseBody==responseBody===%@", responseBody);
//        NSArray *dataArray = responseBody.responseBean.data;
//        NSLog(@"modelmodel==model==%@", dataArray);
//        for (Lists *model in dataArray) {
//            
//            NSLog(@"userinfouserinfo==%@", model.userinfo);
//        }
//        NSLog(@"model.userinfo==%@", model.addtime_f);
    } failure:^(CommonResponseBean *result) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"Cell"];
    }
    Model *model = [_dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = model.assetUrl;
    return cell;
}

- (void)rightBtnClick{
    NSLog(@"清除缓存");
    [JD_CacheTool clearCache];
}
/**
 *   将传递进来的数组转换成Frame数组
 */

- (NSArray *)listFrameWithListModel:(NSArray *)listModelArray{
    NSMutableArray *frames = [NSMutableArray array];
    for (Lists *model in listModelArray) {
        ListModelFrame *frameModel = [[ListModelFrame alloc] init];
        frameModel.list = model;
        [frames addObject:frameModel];
    }
    return frames;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
